/*
 Copyright (C) 2008  Maurizio Monticelli

 This program is free software; you can redistribute it and/or modify it under the terms of the 
 GNU General Public License as published by the Free Software Foundation; either version 2 of the 
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 Public License for more details.

 You should have received a copy of the GNU General Public License along with this program; if not, write 
 to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 
*/

package ch.justdoit.andriodrpncalc.view;

import android.content.Context;
import android.text.Layout.Alignment;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import ch.justdoit.andriodrpncalc.model.CalcNumber;
import ch.justdoit.andriodrpncalc.model.Model;



public class StackDisplay extends AbsoluteLayout {

	private Model mModel;
	private ListView stackList;
	private PromptText prompt;
//	private EditText prompt;
	private TextView status;
	
	public int HEIGHT = 107;
	public int WIDTH = 320;
	private int mStackSize = 3;

	public StackDisplay(Context context, Model _model) {
		super(context);
		mModel = _model;
		stackList = new ListView(context);
//		stackList.setBackgroundColor(0xff29280e);
		mModel.stack.setOnStackChangedListener(onStackChangedListener);
		
				
		prompt = new PromptText(context);

//		prompt = new EditText(context);
//		prompt.setBackgroundColor(0x000000);
//		prompt.setTextColor(0xfff6ef91);
		//prompt.setEnabled(false);
		prompt.setMaxLines(1);
		prompt.setAlignment(Alignment.ALIGN_OPPOSITE);


		mModel.setPrompt(prompt);
		
		//mModel.getPromptNumber().setOnChangedListener(onPromptChangedListenerNew);
		
		status = new TextView(context);
		status.setTextColor(0xffff0000);
		mModel.setOnErrorListener(onErrorListener);
		
		stackList.setStackFromBottom(true);
		int x = 0;
		int y = 0;
		
		int height = IStackItemView.HEIGHT * mStackSize;

		addView(stackList, 
				new AbsoluteLayout.LayoutParams(WIDTH, height, x, y)
		);
		y = y + height;
		
		height = 50;
		//height = prompt.getHeight();
		//prompt.setHeight(height);
		addView(prompt, 
				new AbsoluteLayout.LayoutParams(WIDTH, height, x, y)
		);
		
		addView(status, 
				new AbsoluteLayout.LayoutParams(WIDTH, height, x+5, y+3)
		);
		
		y = y + height;
		
		
		
		HEIGHT = y;
		
		stackList.setOnItemSelectedListener(new OnItemSelectedListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onItemSelected(AdapterView parent, View v, int position,
					long id) {
				//position = 0 - position;
				//position = position + stackList.getCount() - 1;
//				if (mStackSize > mModel.stack.size()) {
//					// if there a few item in the stack than in the display
//					
//					
//					
//					mModel.setCursorPosition(mModel.stack.size() - 1);
//					// or shoud be -1?
//					mModel.setCursorPosition(-1);
//				}
//				else {
					mModel.setCursorPosition(position);
//				}
				mModel.setErrorMessage("stack:"  + mModel.getCursorPosition() + " pos:" + position);
				
			}

			@SuppressWarnings("unchecked")
			@Override
			public void onNothingSelected(AdapterView arg0) {
				
			}
			
		});
		
		refreshStackList();
	}


	private OnErrorListener onErrorListener = new OnErrorListener() {

		@Override
		public void onError() {
			status.setText(mModel.getErrorMessage());
		}
		
	};
	

	
//	private IOnChangedListener onPromptChangedListenerNew = new IOnChangedListener() {
//
//		@Override
//		public void changed() {
//			prompt.addToken(mModel.getPromptNumber().toString());
//		}
//		
//	};
	
	private OnStackChangedListener onStackChangedListener = new OnStackChangedListener() {

		@Override
		public void onChanged() {
			refreshStackList();
		}
		
	};
	
	private void refreshStackList() {
		IStackListAdapter itla = new IStackListAdapter(mContext);


//        int s = mModel.stack.size();
//        if (s < mStackSize) {
//        	s = mStackSize - s;
//        	 for (int i = 0; i < s; i++) {
//             	itla.addItem(new IStackItem(new CalcNumber(mModel), (mStackSize - i) + ": "));
//             }
//
//        }
       
        for (int i = 0; i < mModel.stack.size(); i++ ) {
        	CalcNumber number = mModel.stack.getItem(i);
        	itla.addItem(new IStackItem(number, (mModel.stack.size() - i) + ": "));
        }
        
        stackList.setAdapter(itla); 
	}
	


	public int getStackSize() {
		return mStackSize;
	}

	public void setStackSize(int stackSize) {
		mStackSize = stackSize;
	}
	
	

}
